var Fly = require('flyio/dist/npm/wx');
var fly = new Fly();
fly.config = {
  method: 'POST', //请求方法， GET 、POST ...
  headers: {}, //请求头
  // baseURL: "https://app.ameimeika.com", //请求基地址
  baseURL: 'https://mp.ameimeika.com', //请求基地址（测试服）
  //是否自动将Content-Type为“application/json”的响应数据转化为JSON对象，默认为true
  parseJson: true,
  timeout: '10000' //超时时间
};
fly.interceptors.request.use(request => {
  request.timeout = 30000;
  if (wx.getStorageSync('user_token')) {
    //检查本地缓存是否有token存在没有则重新获取
    request.headers = {
      //设置请求头
      Authorization: 'Bearer ' + wx.getStorageSync('user_token')
    };
    return request;
  }
});

function fly_post(url, data = {}, success, loadingMsg = '加载中') {
  wx.showLoading({
    title: loadingMsg, //提示的内容,
    mask: true //显示透明蒙层，防止触摸穿透,
  });
  fly
    .post(url, data)
    .then(result => {
      wx.hideLoading();
      let res = result.data;
      if (res.status_code == 0) {
        success();
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000,
          mask: false
        });
      }
    })
    .catch(err => {
      console.log(err);
      wx.hideLoading();
      wx.showToast({
        title: '网络错误，请稍后重试',
        icon: 'none',
        duration: 2000,
        mask: false
      });
    });
}
export default fly_post;
